package com.geor.gcv.ict.train;

import java.util.ArrayList;
import java.util.List;

import org.nutz.log.Loggers;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.ml.CvKNearest;
import org.slf4j.Logger;

public class MlKNN implements Ml {
	protected Logger log = Loggers.get();

	CvKNearest knn = new CvKNearest();
	private List<Mat> nearests = new ArrayList<Mat>();
	private int maxK = 2;

	public MlKNN() {
	}

	@Override
	public void load(String path) {
		throw new UnsupportedOperationException("KNN不支持加载配置文件!");
	}

	public CvKNearest getKnn() {
		return knn;
	}

	public int getMaxK() {
		return maxK;
	}

	public List<Mat> getNearests() {
		return nearests;
	}

	@Override
	public void train(Mat trainData, Mat label, String out) {
		knn.train(trainData, label, new Mat(), false, maxK, false);

	}

	@Override
	public float predict(Mat sample) {
		Mat nearest = new Mat(1, maxK, CvType.CV_32FC1);
		float response = knn.find_nearest(sample, maxK, new Mat(), nearest, new Mat());
		nearests.add(nearest);

		return response;
	}

	@Override
	public boolean isSuccess() {
		// TODO Auto-generated method stub
		return false;
	}

}
